package com.jltx.xq.videoeditorlearnpro.permissions.dispatcher;

import android.app.Activity;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

import java.lang.ref.WeakReference;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/12/22.
 * @version V1.0
 * @Description 用户权限申请分发通用类
 */

public abstract class CommonPermissionsDispatcher {

    private  final int requestCode;

    private  String[] permissions = new String[]{};

    public CommonPermissionsDispatcher(int requestCode, String[] permissions) {
        this.requestCode = requestCode;
        if(permissions != null){
            this.permissions = permissions;
        }
    }

    /**
     * 权限调用拦截方法
     */
    public void showPermissionForOperateWithCheck(Activity activity){
        if(permissions == null){
            return;
        }
        //判断是否有这些权限
        if (PermissionUtils.hasSelfPermissions(activity, permissions)) {
            allowForOperater(false);
        } else {
            //是否需要弹出解释对话框
            if (PermissionUtils.shouldShowRequestPermissionRationale(activity, permissions)) {
                rationaleForOperater(new CommonPermissionsDispatcher.CommenPermissionRequest(activity,this, requestCode,permissions));
            } else {
                ActivityCompat.requestPermissions(activity, permissions, requestCode);
            }
        }
    }

    /**
     * 权限结果回调
     * @param target
     * @param requestCode
     * @param grantResults
     */
    public void onRequestPermissionsResult(Activity target, int requestCode, int[] grantResults) {

        //没有可校验的权限
        if(permissions == null){
            return;
        }
        if(requestCode == this.requestCode){
            //用户拒绝
            if (PermissionUtils.getTargetSdkVersion(target) < Build.VERSION_CODES.M && !PermissionUtils.hasSelfPermissions(target, permissions)) {
                deniedForOperater();
                return;
            }

            //判断用户是否允许
            if (PermissionUtils.verifyPermissions(grantResults)) {
                allowForOperater(true);
            } else {
                //判断是否不再提示权限
                if (!PermissionUtils.shouldShowRequestPermissionRationale(target, permissions)) {
                    //提示用户去系统设置里面打开权限
                    neverAskAgainForOperater();
                } else {
                    //用户拒绝
                    deniedForOperater();
                }
            }
        }

    }


    private static final class CommenPermissionRequest implements PermissionRequest {
        private  int requestCode = 0;
        private  String[] permissions = new String[]{};
        private WeakReference<Activity> activityTarget;
        private WeakReference<CommonPermissionsDispatcher> dispatcherTarget;

        private CommenPermissionRequest(Activity activity, CommonPermissionsDispatcher dispatcher, int requestCode, String[] permissions) {
            this.activityTarget = new WeakReference<Activity>(activity);
            this.dispatcherTarget = new WeakReference<CommonPermissionsDispatcher>(dispatcher);
            this.requestCode = requestCode;
            this.permissions = permissions;
        }

        @Override
        public void proceed() {
            Activity target = activityTarget.get();
            if (target == null){
                return;
            }
            ActivityCompat.requestPermissions(target, permissions, requestCode);
        }

        @Override
        public void cancel() {
            CommonPermissionsDispatcher commonPermissionsDispatcher = dispatcherTarget.get();
            if (commonPermissionsDispatcher == null){
                return;
            }
            commonPermissionsDispatcher.deniedForOperater();
        }
    }

    /**
     * 运行权限之后最终回调的方法，调用外部真正跳转方法
     * @param isFromRequestPermission  是否是由系统授权后回调，true：表示callback回调,false:表示直接有权限调用
     */
    public abstract void allowForOperater(boolean isFromRequestPermission);

    /**
     * 向用户说明为什么需要这些权限
     */
    public abstract void rationaleForOperater(final PermissionRequest request);

    /**
     * 用户拒绝的申请回调
     */
    public abstract void deniedForOperater();

    /**
     * 用户勾选了“不再提醒”时调用
     */
    public abstract void neverAskAgainForOperater();


}
